package com.cast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cast.dao.MenuManagementDao;
import com.cast.entity.PageResult;
import com.cast.entity.QueryPageBean;
import com.cast.pojo.Menu;
import com.cast.service.MenuManagementService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = MenuManagementService.class)
@Transactional
public class MenuManagementServiceImpl implements MenuManagementService {
    @Autowired
    private MenuManagementDao menuManagementDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize );
        //判断查询条件是否为空
        if(queryString==null || queryString ==""||queryString.length()==0){
           Page<Menu> aMenu =  menuManagementDao.findAMenu();
           return new PageResult(aMenu.getTotal(),aMenu.getResult());
        }
        //有查询条件查询所有的菜单项包括子项
        Page<Menu> menuByCondition =  menuManagementDao.findByCondition(queryString);
        return new PageResult(menuByCondition.getTotal(),menuByCondition.getResult());

//        return null;
    }

    @Override
    public void add(Menu menu) {
        /*Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId!=0) {
            String path = menuManagementDao.findPath(parentMenuId);
            menu.setPath("/"+path+menu.getPriority());
        }*/
        Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId==0){
            menu.setParentMenuId(null);
        }
        menuManagementDao.add(menu);
    }
}
